package com.systemic.controller;

import com.systemic.core.ResponseResult;
import com.systemic.pojo.Draft;
import com.systemic.pojo.Dynamic;
import com.systemic.service.DraftService;
import com.systemic.service.DynamicService;
import com.systemic.vo.DynamicVo;
import com.systemic.vo.PageVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

import java.util.List;

/**
 * @ClassName: DynamicController
 * @Description:
 * @Author:
 * @CreateTime: 2024-01-05 13:57
 * @Version:
 **/
@RestController
@RequestMapping("/dynamic")
public class DynamicController {

    @Resource
    private DynamicService dynamicService;

    @Resource
    private DraftService draftService;

    /**
     * 发布动态
     * @param images
     * @param uid
     * @param content
     * @param category
     * @return
     */
    @PostMapping("/publish")
    public ResponseResult<String> publish(@RequestParam(value = "images", required = false) MultipartFile[] images,
                                          @RequestParam("uid") Integer uid,
                                          @RequestParam("content") String content,
                                          @RequestParam("category") Integer category,
                                          @RequestParam(value = "subject", required = false) Integer[] subject) throws IOException {
        int row = 0;
        if (content!=null){
            if (category==0){
                // 新增动态
                Dynamic dynamic = new Dynamic();
                dynamic.setUid(uid);
                dynamic.setContent(content);
                dynamic.setCategory(category);
                row = dynamicService.publish(images, dynamic, subject);
            } else if (category==1){
                // 暂存草稿
                Draft draft = new Draft();
                draft.setUid(uid);
                draft.setContent(content);
                draft.setCategory(category);
                row = draftService.staging(images, draft, subject);
            } else {
                System.out.println("尚未扩展该业务");
            }
        }
        if (row==0){
            return ResponseResult.failed("操作失败！");
        }
        return ResponseResult.success("操作成功！");
    }

    /**
     * 查看指定动态
     * @param dynamic
     * @return
     */
    @PostMapping("/select")
    public ResponseResult<DynamicVo> select(@RequestBody DynamicVo dynamic){
        DynamicVo dynamicVo = dynamicService.selectById(dynamic);
        if (dynamicVo==null){
            return ResponseResult.failed("没有查询到动态");
        }
        return ResponseResult.success(dynamicVo);
    }

    /**
     * 更新动态
     * @param id
     * @param content
     * @param images
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestParam("id") Integer id,
                                         @RequestParam("content") String content,
                                         @RequestParam("images") MultipartFile[] images,
                                         @RequestParam("subject") Integer[] subject){
        Dynamic dynamic = new Dynamic();
        int row = 0;
        if (content!=null){
            dynamic.setId(id);
            dynamic.setContent(content);
            row = dynamicService.update(dynamic, images, subject);
        }
        if (row==0){
            return ResponseResult.failed("修改动态失败");
        }
        return ResponseResult.success("修改动态成功");
    }


    /**
     * 删除动态
     * @param dynamic
     * @return
     */
    @PostMapping("/delete")
    public ResponseResult<String> delete(@RequestBody Dynamic dynamic){
        int row = dynamicService.delete(dynamic);
        if (row==0){
            return ResponseResult.failed("删除动态失败");
        }
        return ResponseResult.success("删除动态成功");
    }

    /**
     * 管理端：批量删除
     * @param ids
     * @return
     */
    @GetMapping("/deleteList/{ids}")
    public ResponseResult<String> deleteList(@PathVariable("ids") Integer[] ids){
        int row = dynamicService.deleteList(ids);
        if (row==0){
            return ResponseResult.failed("删除动态失败");
        }
        return ResponseResult.success("删除动态成功");
    }

    /**
     * 客户端：根据用户id获取动态列表
     * 建议改为流式调用方式
     * @param pageVo
     * @return
     */
    @PostMapping("/selectList")
    public ResponseResult<List<Dynamic>> selectList(@RequestBody PageVo pageVo){
        List<Dynamic> dynamicList = dynamicService.selectList(pageVo);
        return ResponseResult.success(dynamicList);
    }


    /**
     * 管理端：获取所有动态
     * @param
     * @return
     */
    @PostMapping("/getList")
    public ResponseResult<List<Dynamic>> getList(@RequestBody PageVo pageVo){
        List<Dynamic> dynamicList = dynamicService.getList(pageVo);
        return ResponseResult.success(dynamicList);
    }



    /**
     * 访问静态资源
     * 通过Nginx实现反向代理： https://blog.csdn.net/God_WZH/article/details/124386390
     */
}
